﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.IO;

namespace GJLib
{
    public class JachiHelper
    {

        static String jachiPath = @"D:\project\五術\data\jachi";

        static String[] MonthGenerals = { "亥","戌","酉","申","未","午","巳","辰","卯","寅","丑","子"};

        public static String getMonthGeneral(DateTime dt)
        {
            DateTime ys = getYearStartDate(dt.Year);
            int ty=dt.Year;
            if(dt.CompareTo(ys)<0)
                ty--;
             Dictionary<String, JObject> jachis = LoadJachiOfYear(ty);
            List<JObject> allJachi = new List<JObject>();
            foreach (var o in  jachis.Values)
                allJachi.Add(o);
            allJachi.Sort((o1, o2) => o1["time"].Value<DateTime>().CompareTo(o2["time"].Value<DateTime>()));
            for (int i = 0; i < 12; i++)
            {
                DateTime ct=allJachi[i * 2+1]["time"].Value<DateTime>();
                if (dt.CompareTo(ct) < 0)
                {
                    return MonthGenerals[i-1];
                }
            }
          throw new Exception("no month general found! "+dt.ToString());
        }
        public static DateTime getYearStartDate(int p)
        {
            Dictionary<String, JObject> jachis = LoadJachiOfYear(p);
            if (jachis.Count != 0)
            {
                DateTime r = jachis["立春"]["time"].Value<DateTime>();
                return r;
            }
            return new DateTime(p, 2, 5);
        }

        static Dictionary<int, Dictionary<String, JObject>> cached = new Dictionary<int, Dictionary<string, JObject>>();
        public static Dictionary<String, JObject> LoadJachiOfYear(int p)
        {
            if (cached.ContainsKey(p) == false)
            {

                String fn = jachiPath + "\\" + p + ".json";
                if (File.Exists(fn))
                {
                    String content = File.ReadAllText(fn);
                    cached[p] = JsonConvert.DeserializeObject<Dictionary<String, JObject>>(content);
                }
                else
                {
                    cached[p] = new Dictionary<String, JObject>();
                }
            }
            return cached[p];
        }


        public static void calTimeSpantoJachi(DateTime dt, out TimeSpan t1, out TimeSpan t2)
        {
            Dictionary<String, JObject> d1 = LoadJachiOfYear(dt.Year);
            Dictionary<String, JObject> d2 = LoadJachiOfYear(dt.Year - 1);
            List<JObject> allJachi = new List<JObject>();

            foreach (var o in d2.Values)
                allJachi.Add(o);
            foreach (var o in d1.Values)
                allJachi.Add(o);
            allJachi.Sort((o1, o2) => o1["time"].Value<DateTime>().CompareTo(o2["time"].Value<DateTime>()));

            int sz = allJachi.Count;
            for (int i = 0; i < sz; i++)
            {
                if (allJachi[i]["type"].Value<String>().Equals("節") == false)
                    continue;
                DateTime datePost = allJachi[i]["time"].Value<DateTime>();
                if (dt.CompareTo(datePost) <= 0)
                {
                    DateTime datepre = allJachi[i - 2]["time"].Value<DateTime>();
                    t1 = dt.Subtract(datepre);
                    t2 = datePost.Subtract(dt);
                    return;
                }
            }
            throw new Exception("Invalid Time :" + dt.ToString("yyyyMMdd"));
        }
    }
}
